package com.example.administrator.xtu_community;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.xtu_community.activity.AddNoteActivity;
import com.example.administrator.xtu_community.bean.Note;
import com.example.administrator.xtu_community.fragment.EmptyFragment;
import com.example.administrator.xtu_community.fragment.SearchFragment;
import com.example.administrator.xtu_community.fragment.HomeFragment;
import com.example.administrator.xtu_community.fragment.MyFragment;
import com.example.administrator.xtu_community.fragment.NewsFragment;
import com.example.administrator.xtu_community.utils.AnimatorUtils;
import com.google.android.material.tabs.TabLayout;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private DrawerLayout drawerLayout;
    String[] tvTabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        initBottomNav();
        Connector.getDatabase();
        List<Note> list =DataSupport.findAll(Note.class);
    }


    private void initBottomNav() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new EmptyFragment());
        fragmentList.add(new SearchFragment());
        fragmentList.add(new MyFragment());
        viewPager.setAdapter(new MainPageAdapter(getSupportFragmentManager(), fragmentList));
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tvTabs = new String[]{"首页", "新闻", "", "发现", "我的"};

        for (int i = 0; i < fragmentList.size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.main_tab_item, null, false);
            TextView tvTab = view.findViewById(R.id.tabText);
            if (i == 0) {
                AnimatorUtils.startTabAnimator(tvTab);
            } else if (i == 2) {
                tvTab.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.tab_add));
                LinearLayout container = view.findViewById(R.id.tab_container);
                container.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, AddNoteActivity.class);
                        startActivity(intent);
                    }
                });
            }
            tvTab.setText(tvTabs[i]);
            Objects.requireNonNull(tabLayout.getTabAt(i)).setCustomView(view);

        }
        tabLayout.setSelectedTabIndicator(0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tabText);
                AnimatorUtils.startTabAnimator(tv);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tabText);
                AnimatorUtils.cancleTabAnimator(tv);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    private static class MainPageAdapter extends FragmentPagerAdapter {
        List<Fragment> fragmentList = null;

        public MainPageAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

}